<?php

namespace App\Http\Controllers;

use App\Models\User;
use Hashids;
use Illuminate\Support\Facades\Config;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class IndexController extends Controller {
	protected $guard_name = 'ATS1524044516';
	/*public function getDevice($agent) {
		if (true == preg_match("/.+Windows.+/", $agent)) {
			return "web设备";
		} elseif (true == preg_match("/.+Macintosh.+/", $agent)) {
			return "mac设备";
		} elseif (true == preg_match("/.+iPad.+/", $agent)) {
			return "iPad设备";
		} elseif (true == preg_match("/.+iPhone.+/", $agent)) {
			return "iPhone设备";
		} elseif (true == preg_match("/.+Android.+/", $agent)) {
			return "Android设备";
		} else {
			return "未知设备";
		}
	}*/
	public function index() {
		$hero = array('no1' => '蝙蝠侠', 'no2' => '超人');
		$hero2 = &$hero;
		$hero2['no1'] = '蜘蛛侠';
		echo '<pre>';
		var_dump($hero);
		var_dump($hero2);
		$str1="11111";
		$str2=&$str1;
		$str2=222;
		var_dump($str1);
		dd($str2);
		//短信发送
		try
		{
			$config = Config::get('info.message_config');
			$easySms = new EasySms($config);

			$re = $easySms->send(13299026194, [
				'content' => '您的验证码${code}，该验证码5分钟内有效，请勿泄漏于他人！',
				'template' => 'SMS_126570108',
				'data' => [
					'code' => 1234,
				],
			]);
			dd($re);
		} catch (NoGatewayAvailableException $e) {
			dd($e->results);
		}
		$ss = Hashids::encode('18');
		echo $ss;
		dd(Hashids::decode($ss));
		//$name = $_SERVER['HTTP_USER_AGENT'];
		//dd($this->getDevice($name));
		$user = User::where('id', 3)->first();
		/*$this->dispatch(new ProcessPodcast($user));

        dd('结束'.date('Y-m-d H:i:s'));*/
		$token = $user->createToken('token')->accessToken;
		// $url = $user->image;
		dd($token);
		/* $getf=DB::table('job_position')->where('parent_id',0)->select('id','name')->get();
			        $data=[];
			        foreach ($getf as $key => $value) {
			        $data[$key]['value']=$value->name;
			        $data[$key]['label']=$value->name;
			        $gets=DB::table('job_position')->where('parent_id',$value->id)->select('id','name')->get();
			        foreach ($gets as $k1 => $v1) {
			        $data[$key]['children'][$k1]['value']=$v1->name;
			        $data[$key]['children'][$k1]['label']=$v1->name;
			        $gett=DB::table('job_position')->where('parent_id',$v1->id)->select('id','name')->get();
			        foreach ($gett as $k2 => $v2) {
			        $data[$key]['children'][$k1]['children'][$k2]['value']=$v2->name;
			        $data[$key]['children'][$k1]['children'][$k2]['label']=$v2->name;
			        }
			        }
		*/
		/* $getf=DB::table('company_property')->where('parent_id',0)->select('id','name')->get();
			        $data=[];
			        foreach ($getf as $key => $value) {
			        $data[$key]['value']=$value->name;
			        $data[$key]['label']=$value->name;
			        $gets=DB::table('company_property')->where('parent_id',$value->id)->select('id','name')->get();
			        foreach ($gets as $k1 => $v1) {
			        $data[$key]['children'][$k1]['value']=$v1->name;
			        $data[$key]['children'][$k1]['label']=$v1->name;
			        }
			        }
		*/
		$user = \Auth::guard('api')->user();
		$user->guard_name = 'ATS1524044516';
		dd($user->can('role_interview_feedback')); //判断用户是否具有这个权限
		dd($user->hasRole('管理员')); //判断用户是否具有这个角色
		$date = 'ATS' . time();
		/*$role = Role::create(['name' => 'writer111','type'=>1,'guard_name'=>$date]);
			        $permission = Permission::create(['name' => 'edit articles111','guard_name'=>$date]);
			        $role->givePermissionTo($permission);
		*/
		//$ss=$permission->assignRole($role);*/
		//$user=User::find(1);
		//$user->guard_name='ATS1523958540';
		/*$role = Role::find(3);
        $ss=$role->givePermissionTo('writer111');*/

		$user = User::find(2);
		$token = $user->createToken('token')->accessToken;
		dd($token);
		$user->guard_name = 'ATS1524047350';
		$de = $user->getAllPermissions()->toArray();
		dd($de);
		$getData = array();
		$roles = Role::where('guard_name', 'ATS1524044516')->pluck('name', 'id');
		$num = 0;
		foreach ($roles as $key => $value) {
			$getData[$num]['id'] = $key;
			$getData[$num]['name'] = $value;
			$Permission = Permission::where('guard_name', 'ATS1524044516')->select('name', 'id')->get();
			$role = roles::find($key);
			$getData[$num]['permission'] = $Permission->toArray();
			$num++;
		}
		dd($getData);
		$ss1 = $user->can('edit articles111');
		dd($ss1);
		//$user->assignRole('writer111');
		/*$permissions = $user->getAllRoles();
        dd($permissions->toArray());*/

		//$ss1=$user->hasRole('writer111');
	}
	public function login() {
		dd('sss');
	}
}
